clear all
set more off
set type double, perm
capture log close

global d = 0

if $d == 0 { 
global topdir "YOUR DIRECTORY"
global datadir "${topdir}/data"
global outdir "${topdir}/output"
cd "${outdir}"
}

use sample group weight race female aafqt aafqt_bin3 ln_wage using "${datadir}/nlsy_final.dta", clear

log using nlsy_yitzhaki.log, replace


/*  ------------------------------------------------------------------------  */
/*               Data Preparation for Yitzhaki Decomposition                  */
/*  ------------------------------------------------------------------------  */

// group data 
** by AAFQT
sort sample race female aafqt
by sample race female aafqt: egen wt_groupsize = total(weight) // group size including sample weight
by sample race female aafqt: egen sum_ln_wage = total(ln_wage*weight)
gen mean_ln_wage = sum_ln_wage/wt_groupsize // weighted avg log wage

** by AAFQT bin3
sort sample race female aafqt_bin3
by sample race female aafqt_bin3: egen wt_groupsize_bin3 = total(weight) // group size including sample weight
by sample race female aafqt_bin3: egen sum_ln_wage_bin3 = total(ln_wage*weight)
gen mean_bin3_ln_wage = sum_ln_wage/wt_groupsize // weighted avg log wage

// tag
egen tag = tag(sample race female aafqt)

// ols
foreach g in WM79 WM97 BM79 BM97 HM79 HM97 WW79 WW97 BW79 BW97 HW79 HW97 {
	reg ln_wage aafqt if group=="`g'" [w=weight]
	scalar ols_`g' = round(_b[aafqt],0.001)
}


/*  ------------------------------------------------------------------------  */
/*               Decomposition by Race/Ethnicity and Gender                   */
/*  ------------------------------------------------------------------------  */

// collapse
keep if tag==1
keep sample race female group wt_groupsize mean_ln_wage aafqt aafqt_bin3
	
	
// calculate beta for each observation
sort sample race female aafqt
by sample race female: gen b = (mean_ln_wage[_n+1]-mean_ln_wage[_n])/(aafqt[_n+1]-aafqt[_n])


// Yitzhaki weight
// use Equation (3) of our paper (incorporating multiple obs per X and sample weights)
** term1 is the quadratic function of CDF, which reaches peak at median
** term2 is the "dispersion", i.e. the diff between the mean of above v.s. the mean of below
** term3 is deltaX, which is constant in most cases (and would be gone in continuous case)

sort sample race female aafqt

by sample race female: gen rsumN = sum(wt_groupsize) // running sum of group size ("weight")
by sample race female: egen sumN = total(wt_groupsize) // total sum of group size
by sample race female: gen rsumNh = sum(wt_groupsize*aafqt) // running sum of group size * X
by sample race female: egen sumNh = total(wt_groupsize*aafqt) // total sum of group size * X

by sample race female: gen mean_aafqt = sumNh/sumN // (weighted) sample mean
by sample race female: egen var_aafqt = total(wt_groupsize*(aafqt-mean_aafqt)^2*(1/sumN)) // (weighted) sample variance

by sample race female: gen term1 = (1/var_aafqt) * (rsumN/sumN) * ((sumN-rsumN)/sumN)
by sample race female: gen term2 = (sumNh - rsumNh)/(sumN - rsumN) - rsumNh/rsumN
by sample race female: gen term3 = aafqt[_n+1] - aafqt[_n]

gen ols_weight = term1 * term2 * term3

drop rsumN sumN rsumNh sumNh mean_aafqt var_aafqt term1 term2 term3


// cumulative contribution
sort sample race female aafqt
gen bw = b*ols_weight
by sample race female: gen ols_run_bw = sum(bw)
by sample race female: egen ols_total_bw = total(bw)
sort sample race female aafqt_bin3 aafqt
by sample race female aafqt_bin3: gen ols_run_bw_bin3 = ols_run_bw[_N]

// cumulative weights
sort sample race female aafqt
by sample race female: gen ols_runwgt = sum(ols_weight)
by sample race female: egen ols_total_weight = total(ols_weight)

// sum weights by bin
sort sample race female aafqt_bin3
by sample race female aafqt_bin3: egen ols_sumwgt_bin3 = total(ols_weight)

// calculate avg. ols estiamtes by bin
sort sample race female aafqt_bin3
by sample race female aafqt_bin3: egen ols_sumbeta_bin3 = total(b*ols_weight)
gen ols_avgbeta_bin3 = ols_sumbeta_bin3/ols_sumwgt_bin3


// counterfactual
egen tag = tag(sample race female aafqt_bin3)
sort race female aafqt_bin3 sample
by race female aafqt_bin3: egen ols_sumwgt_bin3_79 = mean(ols_sumwgt_bin3/(sample==0))
by race female aafqt_bin3: egen ols_sumwgt_bin3_97 = mean(ols_sumwgt_bin3/(sample==1))
sort sample race female 
by sample race female: egen b_ols_bin3_w79 = total(ols_avgbeta_bin3*ols_sumwgt_bin3_79*tag)
by sample race female: egen b_ols_bin3_w97 = total(ols_avgbeta_bin3*ols_sumwgt_bin3_97*tag)


// cumulative contribution
gen bw_w79 = ols_avgbeta_bin3*ols_sumwgt_bin3_79*tag
gen bw_w97 = ols_avgbeta_bin3*ols_sumwgt_bin3_97*tag
sort sample race female aafqt_bin3
by sample race female: gen ols_run_bw_bin3_w79 = sum(bw_w79)
by sample race female: gen ols_run_bw_bin3_w97 = sum(bw_w97)
by sample race female: egen ols_total_bw_bin3_w79 = total(bw_w79)
by sample race female: egen ols_total_bw_bin3_w97 = total(bw_w97)

drop tag bw_w79 bw_w97


// export the statistics
local ols "ols_total_bw b_ols_bin3_w79 b_ols_bin3_w97"
estpost tabstat `ols', by(group) stat(mean) listwise
esttab using "counterfactual.csv", replace cells("`ols'") nonumber nomtitle noobs		


// save
keep sample race female group mean_ln_wage aafqt aafqt_bin3 ///
	b ols_weight ///
	ols_run_bw ols_total_bw ///
	ols_runwgt ols_total_weight ///
	ols_run_bw_bin3 ///
	ols_sumwgt_bin3 ols_avgbeta_bin3 ///
	ols_run_bw_bin3_w79 ols_run_bw_bin3_w97 ///
	ols_total_bw* b_ols_bin3_w79 b_ols_bin3_w97 
	


/*  ------------------------------------------------------------------------  */
/*            			      	 Figure 8                   	 		      */
/*  ------------------------------------------------------------------------  */

// Yitzhaki weights

twoway (scatter ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, msymbol(circle_hollow)) ///
	(scatter ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==3 & female==0) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, lc(dkgreen) bw(0.1)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==3 & female==0, lc(orange_red) bw(0.1)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) scheme(s1color) xtitle("AAFQT bin") ytitle("Yitzhaki weight") ///
	xsc(r(70 230)) xlabel(75(25)225) 
graph export Figure8_WM.pdf, replace
graph export Figure8_WM.eps, replace
graph save Figure8_WM, replace

twoway (scatter ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, msymbol(circle_hollow)) ///
	(scatter ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==3 & female==1) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, lc(dkgreen) bw(0.1)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==3 & female==1, lc(orange_red) bw(0.1)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) scheme(s1color) xtitle("AAFQT bin") ytitle("Yitzhaki weight") ///
	xsc(r(70 230)) xlabel(75(25)225) 
graph export Figure8_WW.pdf, replace
graph export Figure8_WW.eps, replace
graph save Figure8_WW, replace


/*  ------------------------------------------------------------------------  */
/*            			      	 Figure 9                    	 		      */
/*  ------------------------------------------------------------------------  */

// Yitzhaki weights + LOWESS slopes

twoway (lowess mean_ln_wage aafqt if sample==0 & race==3 & female==0, lc(dkgreen) bw(0.1) lp(-)) ///
	(lowess mean_ln_wage aafqt if sample==1 & race==3 & female==0, lc(orange_red) bw(0.1) lp(-)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, lc(dkgreen) bw(0.1) yaxis(2)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==3 & female==0, lc(orange_red) bw(0.1) yaxis(2)), ///
	legend(order(1 "NLSY-79: Log wage" 2 "NLSY-97: Log wage" 3 "NLSY-79: Weight" 4 "NLSY-97: Weight")) scheme(s1color) ///
	xline(178,lc(gs10) lp(dot)) xtitle("AAFQT bin") ytitle("100 * Average Log wage") ytitle("Yitzhaki weight", axis(2)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(220 340)) ylabel(220(30)340) ylabel(0(0.01)0.05, axis(2))
graph export Figure9_WM.pdf, replace
graph export Figure9_WM.eps, replace
graph save Figure9_WM, replace

twoway (lowess mean_ln_wage aafqt if sample==0 & race==3 & female==1, lc(dkgreen) bw(0.1) lp(-)) ///
	(lowess mean_ln_wage aafqt if sample==1 & race==3 & female==1, lc(orange_red) bw(0.1) lp(-)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, lc(dkgreen) bw(0.1) yaxis(2)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==3 & female==1, lc(orange_red) bw(0.1) yaxis(2)), ///
	legend(order(1 "NLSY-79: Log wage" 2 "NLSY-97: Log wage" 3 "NLSY-79: Weight" 4 "NLSY-97: Weight")) scheme(s1color) ///
	xline(178,lc(gs10) lp(dot)) xtitle("AAFQT bin") ytitle("100 * Average Log wage") ytitle("Yitzhaki weight", axis(2)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(200 320)) ylabel(200(30)320) ylabel(0(0.01)0.05, axis(2))
graph export Figure9_WW.pdf, replace
graph export Figure9_WW.eps, replace
graph save Figure9_WW, replace


/*  ------------------------------------------------------------------------  */
/*            			      	 Figure 10                    	 		      */
/*  ------------------------------------------------------------------------  */

// cumulative contribution to OLS

foreach g in WM79 WM97 BM79 BM97 HM79 HM97 WW79 WW97 BW79 BW97 HW79 HW97 {
	local ols`g' = scalar(ols_`g')
}

twoway (scatter ols_run_bw_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_run_bw_bin3 aafqt_bin3 if sample==1 & race==3 & female==0, msize(small)) ///
	(lowess ols_run_bw aafqt if sample==0 & race==3 & female==0, lc(dkgreen) bw(0.3)) ///
	(lowess ols_run_bw aafqt if sample==1 & race==3 & female==0, lc(orange_red) bw(0.3)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) ///
	scheme(s1color) xtitle("AAFQT bin") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWM79',lc(dkgreen) lp(dot)) yline(`olsWM97',lc(orange_red) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(-0.3 0.9)) ylabel(-0.2(0.2)0.8)
graph export Figure10L_WM.pdf, replace
graph export Figure10L_WM.eps, replace
graph save Figure10L_WM, replace

twoway (scatter ols_run_bw_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_run_bw_bin3 aafqt_bin3 if sample==1 & race==3 & female==1, msize(small)) ///
	(lowess ols_run_bw aafqt if sample==0 & race==3 & female==1, lc(dkgreen) bw(0.3)) ///
	(lowess ols_run_bw aafqt if sample==1 & race==3 & female==1, lc(orange_red) bw(0.3)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) ///
	scheme(s1color) xtitle("AAFQT bin") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWW79',lc(dkgreen) lp(dot)) yline(`olsWW97',lc(orange_red) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(-0.3 0.9)) ylabel(-0.2(0.2)0.8)
graph export Figure10L_WW.pdf, replace
graph export Figure10L_WW.eps, replace
graph save Figure10L_WW, replace	


// cumulative contribution to OLS with counterfactuals

foreach g in WM79 WM97 BM79 BM97 HM79 HM97 WW79 WW97 BW79 BW97 HW79 HW97 {
	local ols`g' = scalar(ols_`g')
	sum b_ols_bin3_w79 if group=="`g'"
	local ols_w79`g' = r(mean)
	sum b_ols_bin3_w97 if group=="`g'"
	local ols_w97`g' = r(mean)
}

twoway (scatter ols_run_bw_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_run_bw_bin3_w79 aafqt_bin3 if sample==1 & race==3 & female==0, mc(midblue) msize(small)) ///
	(lowess ols_run_bw aafqt if sample==0 & race==3 & female==0, lc(dkgreen) bw(0.3)) ///
	(lowess ols_run_bw_bin3_w79 aafqt if sample==1 & race==3 & female==0, lc(midblue) bw(0.3)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97 w/ Weight79: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97 w/ Weight79: smoothed")) ///
	scheme(s1color) xtitle("AAFQT bin") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWM79',lc(dkgreen) lp(dot)) yline(`ols_w79WM97',lc(navy) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(-0.3 0.9)) ylabel(-0.2(0.2)0.8)
graph export Figure10R_WM.pdf, replace
graph export Figure10R_WM.eps, replace
graph save Figure10R_WM, replace	

twoway (scatter ols_run_bw_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_run_bw_bin3_w79 aafqt_bin3 if sample==1 & race==3 & female==1, mc(midblue) msize(small)) ///
	(lowess ols_run_bw aafqt if sample==0 & race==3 & female==1, lc(dkgreen) bw(0.3)) ///
	(lowess ols_run_bw_bin3_w79 aafqt if sample==1 & race==3 & female==1, lc(midblue) bw(0.3)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97 w/ Weight79: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97 w/ Weight79: smoothed")) ///
	scheme(s1color) xtitle("AAFQT bin") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWW79',lc(dkgreen) lp(dot)) yline(`ols_w79WW97',lc(navy) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(-0.3 0.9)) ylabel(-0.2(0.2)0.8)
graph export Figure10R_WW.pdf, replace
graph export Figure10R_WW.eps, replace
graph save Figure10R_WW, replace	


/*  ------------------------------------------------------------------------  */
/*            			      	 Figure A3                   	 		      */
/*  ------------------------------------------------------------------------  */

// Pairwise slopes

twoway (scatter ols_avgbeta_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, msymbol(circle_hollow)) ///
	(scatter ols_avgbeta_bin3 aafqt_bin3 if sample==1 & race==3 & female==0), ///
	xlabel(75(25)225) yline(0,lc(gs10)) legend(order(1 "NLSY-79" 2 "NLSY-97")) scheme(s1color) ///
	aspectratio(.8) xtitle("AAFQT bin") ytitle("Pairwise slope") title("White Non-Hispanic Men") name(g1,replace)
twoway (scatter ols_avgbeta_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, msymbol(circle_hollow)) ///
	(scatter ols_avgbeta_bin3 aafqt_bin3 if sample==1 & race==3 & female==1), ///
	xlabel(75(25)225) yline(0,lc(gs10)) legend(order(1 "NLSY-79" 2 "NLSY-97")) scheme(s1color) ///
	aspectratio(.8) xtitle("AAFQT bin") ytitle("Pairwise slope") title("White Non-Hispanic Women") name(g2,replace)
grc1leg g1 g2, legendfrom(g1) ycommon col(2) scheme(s1color) 
graph export FigureA3.pdf, replace
graph export FigureA3.eps, replace
graph save FigureA3, replace


/*  ------------------------------------------------------------------------  */
/*            			      	 Figure E2                   	 		      */
/*  ------------------------------------------------------------------------  */

// Yitzhaki weights

twoway (scatter ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==3 & female==0, msize(small)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, lc(dkgreen) bw(0.1)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==3 & female==0, lc(orange_red) bw(0.1)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) scheme(s1color) ///
	xtitle("AAFQT bin") ytitle("Yitzhaki weight") title("White Non-Hispanic Men", size(small)) ///
	xsc(r(70 230)) xlabel(75(25)225) aspectratio(.8) name(g1,replace)
twoway (scatter ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==3 & female==1, msize(small)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, lc(dkgreen) bw(0.1)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==3 & female==1, lc(orange_red) bw(0.1)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) scheme(s1color) ///
	xtitle("AAFQT bin") ytitle("Yitzhaki weight") title("White Non-Hispanic Women", size(small)) ///
	xsc(r(70 230)) xlabel(75(25)225) aspectratio(.8) name(g2,replace)
twoway (scatter ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==2 & female==0, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==2 & female==0, msize(small)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==2 & female==0, lc(dkgreen) bw(0.1)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==2 & female==0, lc(orange_red) bw(0.1)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) scheme(s1color) ///
	xtitle("AAFQT bin") ytitle("Yitzhaki weight") title("Black Non-Hispanic Men", size(small)) ///
	xsc(r(70 230)) xlabel(75(25)225) aspectratio(.8) name(g3,replace)
twoway (scatter ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==2 & female==1, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==2 & female==1, msize(small)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==2 & female==1, lc(dkgreen) bw(0.1)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==2 & female==1, lc(orange_red) bw(0.1)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) scheme(s1color) ///
	xtitle("AAFQT bin") ytitle("Yitzhaki weight") title("Black Non-Hispanic Women", size(small)) ///
	xsc(r(70 230)) xlabel(75(25)225) aspectratio(.8) name(g4,replace)
twoway (scatter ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==1 & female==0, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==1 & female==0, msize(small)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==1 & female==0, lc(dkgreen) bw(0.1)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==1 & female==0, lc(orange_red) bw(0.1)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) scheme(s1color) ///
	xtitle("AAFQT bin") ytitle("Yitzhaki weight") title("Hispanic Men", size(small)) ///
	xsc(r(70 230)) xlabel(75(25)225) aspectratio(.8) name(g5,replace)
twoway (scatter ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==1 & female==1, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==1 & female==1, msize(small)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==1 & female==1, lc(dkgreen) bw(0.1)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==1 & female==1, lc(orange_red) bw(0.1)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) scheme(s1color) ///
	xtitle("AAFQT bin") ytitle("Yitzhaki weight") title("Hispanic Women", size(small)) ///
	xsc(r(70 230)) xlabel(75(25)225) aspectratio(.8) name(g6,replace)

grc1leg g1 g3 g5 g2 g4 g6, legendfrom(g1) ycommon col(3) scheme(s1color) 
graph export FigureE2.pdf, replace
graph export FigureE2.eps, replace
graph save FigureE2, replace


/*  ------------------------------------------------------------------------  */
/*            			      	 Figure E3                   	 		      */
/*  ------------------------------------------------------------------------  */

// Yitzhaki weights + LOWESS slopes

twoway (lowess mean_ln_wage aafqt if sample==0 & race==3 & female==0, lc(dkgreen) bw(0.1) lp(-)) ///
	(lowess mean_ln_wage aafqt if sample==1 & race==3 & female==0, lc(orange_red) bw(0.1) lp(-)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, lc(dkgreen) bw(0.1) yaxis(2)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==3 & female==0, lc(orange_red) bw(0.1) yaxis(2)), ///
	legend(order(1 "NLSY-79: Log wage" 2 "NLSY-97: Log wage" 3 "NLSY-79: Weight" 4 "NLSY-97: Weight")) scheme(s1color) ///
	xtitle("AAFQT bin") ytitle("100 * Average Log wage") ytitle("Yitzhaki weight", axis(2)) ///
	xsc(r(70 230)) xlabel(75(25)225) title("White Non-Hispanic Men", size(small)) aspectratio(.8) name(g1,replace)
twoway (lowess mean_ln_wage aafqt if sample==0 & race==3 & female==1, lc(dkgreen) bw(0.1) lp(-)) ///
	(lowess mean_ln_wage aafqt if sample==1 & race==3 & female==1, lc(orange_red) bw(0.1) lp(-)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, lc(dkgreen) bw(0.1) yaxis(2)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==3 & female==1, lc(orange_red) bw(0.1) yaxis(2)), ///
	legend(order(1 "NLSY-79: Log wage" 2 "NLSY-97: Log wage" 3 "NLSY-79: Weight" 4 "NLSY-97: Weight")) scheme(s1color) ///
	xtitle("AAFQT bin") ytitle("100 * Average Log wage") ytitle("Yitzhaki weight", axis(2)) ///
	xsc(r(70 230)) xlabel(75(25)225) title("White Non-Hispanic Women", size(small)) aspectratio(.8) name(g2,replace)
twoway (lowess mean_ln_wage aafqt if sample==0 & race==2 & female==0, lc(dkgreen) bw(0.1) lp(-)) ///
	(lowess mean_ln_wage aafqt if sample==1 & race==2 & female==0, lc(orange_red) bw(0.1) lp(-)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==2 & female==0, lc(dkgreen) bw(0.1) yaxis(2)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==2 & female==0, lc(orange_red) bw(0.1) yaxis(2)), ///
	legend(order(1 "NLSY-79: Log wage" 2 "NLSY-97: Log wage" 3 "NLSY-79: Weight" 4 "NLSY-97: Weight")) scheme(s1color) ///
	xtitle("AAFQT bin") ytitle("100 * Average Log wage") ytitle("Yitzhaki weight", axis(2)) ///
	xsc(r(70 230)) xlabel(75(25)225) title("Black Non-Hispanic Men", size(small)) aspectratio(.8) name(g3,replace)
twoway (lowess mean_ln_wage aafqt if sample==0 & race==2 & female==1, lc(dkgreen) bw(0.1) lp(-)) ///
	(lowess mean_ln_wage aafqt if sample==1 & race==2 & female==1, lc(orange_red) bw(0.1) lp(-)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==2 & female==1, lc(dkgreen) bw(0.1) yaxis(2)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==2 & female==1, lc(orange_red) bw(0.1) yaxis(2)), ///
	legend(order(1 "NLSY-79: Log wage" 2 "NLSY-97: Log wage" 3 "NLSY-79: Weight" 4 "NLSY-97: Weight")) scheme(s1color) ///
	xtitle("AAFQT bin") ytitle("100 * Average Log wage") ytitle("Yitzhaki weight", axis(2)) ///
	xsc(r(70 230)) xlabel(75(25)225) title("Black Non-Hispanic Women", size(small)) aspectratio(.8) name(g4,replace)
twoway (lowess mean_ln_wage aafqt if sample==0 & race==1 & female==0, lc(dkgreen) bw(0.1) lp(-)) ///
	(lowess mean_ln_wage aafqt if sample==1 & race==1 & female==0, lc(orange_red) bw(0.1) lp(-)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==1 & female==0, lc(dkgreen) bw(0.1) yaxis(2)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==1 & female==0, lc(orange_red) bw(0.1) yaxis(2)), ///
	legend(order(1 "NLSY-79: Log wage" 2 "NLSY-97: Log wage" 3 "NLSY-79: Weight" 4 "NLSY-97: Weight")) scheme(s1color) ///
	xtitle("AAFQT bin") ytitle("100 * Average Log wage") ytitle("Yitzhaki weight", axis(2)) ///
	xsc(r(70 230)) xlabel(75(25)225) title("Hispanic Men", size(small)) aspectratio(.8) name(g5,replace)
twoway (lowess mean_ln_wage aafqt if sample==0 & race==1 & female==1, lc(dkgreen) bw(0.1) lp(-)) ///
	(lowess mean_ln_wage aafqt if sample==1 & race==1 & female==1, lc(orange_red) bw(0.1) lp(-)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==0 & race==1 & female==1, lc(dkgreen) bw(0.1) yaxis(2)) ///
	(lowess ols_sumwgt_bin3 aafqt_bin3 if sample==1 & race==1 & female==1, lc(orange_red) bw(0.1) yaxis(2)), ///
	legend(order(1 "NLSY-79: Log wage" 2 "NLSY-97: Log wage" 3 "NLSY-79: Weight" 4 "NLSY-97: Weight")) scheme(s1color) ///
	xtitle("AAFQT bin") ytitle("100 * Average Log wage") ytitle("Yitzhaki weight", axis(2)) ///
	xsc(r(70 230)) xlabel(75(25)225) title("Hispanic Women", size(small)) aspectratio(.8) name(g6,replace)
	
grc1leg g1 g3 g5 g2 g4 g6, legendfrom(g1) ycommon col(3) scheme(s1color) 
graph export FigureE3.pdf, replace
graph export FigureE3.eps, replace
graph save FigureE3, replace


/*  ------------------------------------------------------------------------  */
/*            			      	 Figure E4                    	 		      */
/*  ------------------------------------------------------------------------  */

// cumulative contribution to OLS

foreach g in WM79 WM97 BM79 BM97 HM79 HM97 WW79 WW97 BW79 BW97 HW79 HW97 {
	local ols`g' = scalar(ols_`g')
}

capture graph drop _all
twoway (lowess ols_run_bw aafqt if sample==0 & race==3 & female==0, bw(0.3)) ///
	(lowess ols_run_bw aafqt if sample==1 & race==3 & female==0, bw(0.3)), ///
	legend(order(1 "NLSY-79" 2 "NLSY-97")) scheme(s1color) xtitle("AAFQT") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWM79',lc(dkgreen) lp(dot)) yline(`olsWM97',lc(orange_red) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) title("White Non-Hispanic Men", size(small)) aspectratio(.8) name(g1,replace)
twoway (lowess ols_run_bw aafqt if sample==0 & race==3 & female==1, bw(0.3)) ///
	(lowess ols_run_bw aafqt if sample==1 & race==3 & female==1, bw(0.3)), ///
	legend(order(1 "NLSY-79" 2 "NLSY-97")) scheme(s1color) xtitle("AAFQT") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWW79',lc(dkgreen) lp(dot)) yline(`olsWW97',lc(orange_red) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) title("White Non-Hispanic Women", size(small)) aspectratio(.8) name(g2,replace)
twoway (lowess ols_run_bw aafqt if sample==0 & race==2 & female==0, bw(0.3)) ///
	(lowess ols_run_bw aafqt if sample==1 & race==2 & female==0, bw(0.3)), ///
	legend(order(1 "NLSY-79" 2 "NLSY-97")) scheme(s1color) xtitle("AAFQT") ytitle("Cumulative contribution to OLS") ///
	yline(`olsBM79',lc(dkgreen) lp(dot)) yline(`olsBM97',lc(orange_red) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) title("Black Non-Hispanic Men", size(small)) aspectratio(.8) name(g3,replace)
twoway (lowess ols_run_bw aafqt if sample==0 & race==2 & female==1, bw(0.3)) ///
	(lowess ols_run_bw aafqt if sample==1 & race==2 & female==1, bw(0.3)), ///
	legend(order(1 "NLSY-79" 2 "NLSY-97")) scheme(s1color) xtitle("AAFQT") ytitle("Cumulative contribution to OLS") ///
	yline(`olsBW79',lc(dkgreen) lp(dot)) yline(`olsBW97',lc(orange_red) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) title("Black Non-Hispanic Women", size(small)) aspectratio(.8) name(g4,replace)	
twoway (lowess ols_run_bw aafqt if sample==0 & race==1 & female==0, bw(0.3)) ///
	(lowess ols_run_bw aafqt if sample==1 & race==1 & female==0, bw(0.3)), ///
	legend(order(1 "NLSY-79" 2 "NLSY-97")) scheme(s1color) xtitle("AAFQT") ytitle("Cumulative contribution to OLS") ///
	yline(`olsHM79',lc(dkgreen) lp(dot)) yline(`olsHM97',lc(orange_red) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) title("Hispanic Men", size(small)) aspectratio(.8) name(g5,replace)
twoway (lowess ols_run_bw aafqt if sample==0 & race==1 & female==1, bw(0.3)) ///
	(lowess ols_run_bw aafqt if sample==1 & race==1 & female==1, bw(0.3)), ///
	legend(order(1 "NLSY-79" 2 "NLSY-97")) scheme(s1color) xtitle("AAFQT") ytitle("Cumulative contribution to OLS") ///
	yline(`olsHW79',lc(dkgreen) lp(dot)) yline(`olsHW97',lc(orange_red) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) title("Hispanic Women", size(small)) aspectratio(.8) name(g6,replace)	

grc1leg g1 g3 g5 g2 g4 g6, legendfrom(g1) ycommon col(3) scheme(s1color) 
graph export FigureE4.pdf, replace
graph export FigureE4.eps, replace
graph save FigureE4, replace


/*  ------------------------------------------------------------------------  */
/*            			      	 Figure F1                    	 		      */
/*  ------------------------------------------------------------------------  */

// cumulative contribution to OLS

foreach g in WM79 WM97 BM79 BM97 HM79 HM97 WW79 WW97 BW79 BW97 HW79 HW97 {
	local ols`g' = scalar(ols_`g')
}

twoway (scatter ols_run_bw_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_run_bw_bin3 aafqt_bin3 if sample==1 & race==3 & female==0, msize(small)) ///
	(lowess ols_run_bw aafqt if sample==0 & race==3 & female==0, lc(dkgreen) bw(0.5)) ///
	(lowess ols_run_bw aafqt if sample==1 & race==3 & female==0, lc(orange_red) bw(0.5)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) ///
	scheme(s1color) xtitle("AAFQT bin") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWM79',lc(dkgreen) lp(dot)) yline(`olsWM97',lc(orange_red) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(-0.3 0.9)) ylabel(-0.2(0.2)0.8)
graph export FigureF1L_WM.pdf, replace
graph export FigureF1L_WM.eps, replace
graph save FigureF1L_WM, replace

twoway (scatter ols_run_bw_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_run_bw_bin3 aafqt_bin3 if sample==1 & race==3 & female==1, msize(small)) ///
	(lowess ols_run_bw aafqt if sample==0 & race==3 & female==1, lc(dkgreen) bw(0.5)) ///
	(lowess ols_run_bw aafqt if sample==1 & race==3 & female==1, lc(orange_red) bw(0.5)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) ///
	scheme(s1color) xtitle("AAFQT bin") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWW79',lc(dkgreen) lp(dot)) yline(`olsWW97',lc(orange_red) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(-0.3 0.9)) ylabel(-0.2(0.2)0.8)
graph export FigureF1L_WW.pdf, replace
graph export FigureF1L_WW.eps, replace
graph save FigureF1L_WW, replace


// cumulative contribution to OLS with counterfactuals

foreach g in WM79 WM97 BM79 BM97 HM79 HM97 WW79 WW97 BW79 BW97 HW79 HW97 {
	local ols`g' = scalar(ols_`g')
	sum b_ols_bin3_w79 if group=="`g'"
	local ols_w79`g' = r(mean)
	sum b_ols_bin3_w97 if group=="`g'"
	local ols_w97`g' = r(mean)
}

twoway (scatter ols_run_bw_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_run_bw_bin3_w79 aafqt_bin3 if sample==1 & race==3 & female==0, mc(midblue) msize(small)) ///
	(lowess ols_run_bw aafqt if sample==0 & race==3 & female==0, lc(dkgreen) bw(0.5)) ///
	(lowess ols_run_bw_bin3_w79 aafqt if sample==1 & race==3 & female==0, lc(midblue) bw(0.5)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97 w/ Weight79: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97 w/ Weight79: smoothed")) ///
	scheme(s1color) xtitle("AAFQT bin") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWM79',lc(dkgreen) lp(dot)) yline(`ols_w79WM97',lc(navy) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(-0.3 0.9)) ylabel(-0.2(0.2)0.8)
graph export FigureF1R_WM.pdf, replace
graph export FigureF1R_WM.eps, replace
graph save FigureF1R_WM, replace

twoway (scatter ols_run_bw_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_run_bw_bin3_w79 aafqt_bin3 if sample==1 & race==3 & female==1, mc(midblue) msize(small)) ///
	(lowess ols_run_bw aafqt if sample==0 & race==3 & female==1, lc(dkgreen) bw(0.5)) ///
	(lowess ols_run_bw_bin3_w79 aafqt if sample==1 & race==3 & female==1, lc(midblue) bw(0.5)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97 w/ Weight79: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97 w/ Weight79: smoothed")) ///
	scheme(s1color) xtitle("AAFQT bin") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWW79',lc(dkgreen) lp(dot)) yline(`ols_w79WW97',lc(navy) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(-0.3 0.9)) ylabel(-0.2(0.2)0.8)
graph export FigureF1R_WW.pdf, replace
graph export FigureF1R_WW.eps, replace
graph save FigureF1R_WW, replace


/*  ------------------------------------------------------------------------  */
/*            			      	 Figure F2                    	 		      */
/*  ------------------------------------------------------------------------  */

// cumulative contribution to OLS

foreach g in WM79 WM97 BM79 BM97 HM79 HM97 WW79 WW97 BW79 BW97 HW79 HW97 {
	local ols`g' = scalar(ols_`g')
}

twoway (scatter ols_run_bw_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_run_bw_bin3 aafqt_bin3 if sample==1 & race==3 & female==0, msize(small)) ///
	(lowess ols_run_bw aafqt if sample==0 & race==3 & female==0, lc(dkgreen) bw(0.8)) ///
	(lowess ols_run_bw aafqt if sample==1 & race==3 & female==0, lc(orange_red) bw(0.8)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) ///
	scheme(s1color) xtitle("AAFQT bin") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWM79',lc(dkgreen) lp(dot)) yline(`olsWM97',lc(orange_red) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(-0.3 0.9)) ylabel(-0.2(0.2)0.8)
graph export FigureF2L_WM.pdf, replace
graph export FigureF2L_WM.eps, replace
graph save FigureF2L_WM, replace

twoway (scatter ols_run_bw_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_run_bw_bin3 aafqt_bin3 if sample==1 & race==3 & female==1, msize(small)) ///
	(lowess ols_run_bw aafqt if sample==0 & race==3 & female==1, lc(dkgreen) bw(0.8)) ///
	(lowess ols_run_bw aafqt if sample==1 & race==3 & female==1, lc(orange_red) bw(0.8)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97: smoothed")) ///
	scheme(s1color) xtitle("AAFQT bin") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWW79',lc(dkgreen) lp(dot)) yline(`olsWW97',lc(orange_red) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(-0.3 0.9)) ylabel(-0.2(0.2)0.8)
graph export FigureF2L_WW.pdf, replace
graph export FigureF2L_WW.eps, replace
graph save FigureF2L_WW, replace


// cumulative contribution to OLS with counterfactuals

foreach g in WM79 WM97 BM79 BM97 HM79 HM97 WW79 WW97 BW79 BW97 HW79 HW97 {
	local ols`g' = scalar(ols_`g')
	sum b_ols_bin3_w79 if group=="`g'"
	local ols_w79`g' = r(mean)
	sum b_ols_bin3_w97 if group=="`g'"
	local ols_w97`g' = r(mean)
}

twoway (scatter ols_run_bw_bin3 aafqt_bin3 if sample==0 & race==3 & female==0, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_run_bw_bin3_w79 aafqt_bin3 if sample==1 & race==3 & female==0, mc(midblue) msize(small)) ///
	(lowess ols_run_bw aafqt if sample==0 & race==3 & female==0, lc(dkgreen) bw(0.8)) ///
	(lowess ols_run_bw_bin3_w79 aafqt if sample==1 & race==3 & female==0, lc(midblue) bw(0.8)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97 w/ Weight79: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97 w/ Weight79: smoothed")) ///
	scheme(s1color) xtitle("AAFQT bin") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWM79',lc(dkgreen) lp(dot)) yline(`ols_w79WM97',lc(navy) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(-0.3 0.9)) ylabel(-0.2(0.2)0.8)
graph export FigureF2R_WM.pdf, replace
graph export FigureF2R_WM.eps, replace
graph save FigureF2R_WM, replace

twoway (scatter ols_run_bw_bin3 aafqt_bin3 if sample==0 & race==3 & female==1, msymbol(circle_hollow) msize(small)) ///
	(scatter ols_run_bw_bin3_w79 aafqt_bin3 if sample==1 & race==3 & female==1, mc(midblue) msize(small)) ///
	(lowess ols_run_bw aafqt if sample==0 & race==3 & female==1, lc(dkgreen) bw(0.8)) ///
	(lowess ols_run_bw_bin3_w79 aafqt if sample==1 & race==3 & female==1, lc(midblue) bw(0.8)), ///
	legend(order(1 "NLSY-79: binned" 2 "NLSY-97 w/ Weight79: binned" 3 "NLSY-79: smoothed" 4 "NLSY-97 w/ Weight79: smoothed")) ///
	scheme(s1color) xtitle("AAFQT bin") ytitle("Cumulative contribution to OLS") ///
	yline(`olsWW79',lc(dkgreen) lp(dot)) yline(`ols_w79WW97',lc(navy) lp(dot)) ///
	xsc(r(70 230)) xlabel(75(25)225) ysc(r(-0.3 0.9)) ylabel(-0.2(0.2)0.8)
graph export FigureF2R_WW.pdf, replace
graph export FigureF2R_WW.eps, replace
graph save FigureF2R_WW, replace




log close

exit






